from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.core.models.enums.order import OrderStatus

from .base import TimedBaseModel


class Order(TimedBaseModel):
    __tablename__ = "order"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    products = relationship("Product", back_populates="order")
    summ: Mapped[float] = mapped_column(nullable=False)
    promocode = relationship("Promocode", back_populates="orders")
    promocode_id: Mapped[int] = mapped_column(ForeignKey("promocode.id"), nullable=True)
    user = relationship("User", back_populates="orders")
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))

    def __repr__(self):
        return f"<Заказ №{self.id} {self.created_at}>"

    def __str__(self):
        return self.__repr__()
